<script setup lang="ts">
import useBar from './useBar'
import type { MenuType as TabType } from '@/routers/type'
import homeImage from '@/assets/images/global/home.svg'

const props = defineProps({
  tabs: {
    type: Array as PropType<Array<TabType>>,
    default: () => [],
  },
  route: {
    type: Object as PropType<any>,
    default: () => {},
  },
  homePath: {
    type: String as PropType<string>,
    default: () => '/',
  },
})
const emit = defineEmits(['updateTabs', 'tabClick', 'removeTabs'])

function goHome() {
  tabItemClick({
    name: 'home',
    path: props.homePath,
    meta: {
      activeMenu: props.homePath,
    },
  })
}

const {
  tabsTranlateLocation,
  maxMoveDistance,
  translateX,
  tabItemClick,
  tabsRef,
  tabsOutRef,
  handleDel,
  setActiveClass,
  closeAllTab,
  deleteByPath,
  canMove,
} = useBar(toRef(props, 'tabs'), emit, toRef(props, 'route'), props.homePath)

defineExpose({
  deleteByPath,
})
</script>

<template>
  <div class="ds-tab-bar">
    <img class="home-image" :src="homeImage" slt="" @click="goHome">
    <div class="icon-block">
      <i
        v-show="canMove && -tabsTranlateLocation > 0"
        class="iconfont prev-icon"
        @click="translateX('pre')"
      >
        &#xe794;
      </i>
    </div>

    <div ref="tabsOutRef" class="tab-box">
      <div ref="tabsRef" class="tab-panel">
        <div
          v-for="(tab, index) in tabs"
          :key="tab.name"
          class="tab-item"
          :class="setActiveClass(tab)"
          @click="tabItemClick(tab)"
        >
          <span class="item-dot" />
          <span class="name"> {{ tab.meta?.title || tab.name }} </span>
          <div class="item-close">
            <i class="iconfont icon-close" @click.stop="handleDel(tab, index)">
              &#xe600;
            </i>
          </div>
        </div>
      </div>
    </div>
    <div class="icon-block">
      <i
        v-show="canMove && maxMoveDistance < tabsTranlateLocation"
        class="iconfont next-icon"
        @click="translateX('next')"
      >
        &#xe636;
      </i>
    </div>
    <el-dropdown v-if="tabs.length">
      <span>
        <i class="iconfont">&#xe601;</i>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="closeAllTab('all')">
            关闭所有
          </el-dropdown-item>
          <el-dropdown-item @click="closeAllTab('other')">
            关闭其它
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<style lang="scss">
@import '@/styles/common.scss';
.ds-tab-bar {
  flex-shrink: 0;
  height: 46px;
  line-height: 46px;
  box-sizing: border-box;
  box-shadow: 0 3px 3px #e3e7ef;
  background-color: white;
  color: $contentColor;
  padding: 0 20px;
  @include vertical-center;
  justify-content: flex-start;
  .home-image {
    height: 24px;
    width: 24px;
    cursor: pointer;
  }
  .icon-block {
    width: 16px;
    user-select: none;
    margin: 0 8px;
  }
  .prev-icon,
  .next-icon {
    cursor: pointer;
    &:hover {
      color: $primary;
    }
  }
  .home-svg {
    cursor: pointer;
    transform: scale(0.5);
  }
  .tab-box {
    width: calc(100% - 50px - 64px);
    display: flex;
    align-items: center;
    height: 32px;
    overflow: hidden;
    position: relative;
    .tab-panel {
      position: absolute;
      height: 32px;
      display: flex;
      align-items: center;
      transition: all 0.5s;
      left: 0;
      overflow: visible;
      white-space: nowrap;
      .tab-item {
        flex-shrink: 0;
        cursor: default;
        @include vertical-center;
        color: $contentColor;
        border: 1px solid #e3e7ef;
        border-radius: 10px;
        box-sizing: border-box;
        height: 100%;
        margin-right: 4px;
        padding: 0 12px;
        line-height: 30px;
        &:last-child {
          margin-right: 0;
        }
        &.tab-active {
          .item-dot {
            background-color: $primary;
          }
          .name {
            color: $primary;
          }
        }
        .item-close {
          width: 10px;
        }
        .icon-close {
          font-size: 16px;
          //transform: scale(0.65);
          color: #b5b5b5;
          cursor: pointer;
          display: inline-block;
          vertical-align: middle;
          &:hover {
            color: $primary;
          }
        }
        .item-dot {
          width: 12px;
          height: 12px;
          border-radius: 50%;
          background-color: #e8eaec;
        }
        .name {
          display: inline-block;
          margin: 0 12px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
